package com.cn.springcloudwechat.controller.vaccinestation;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.utils.Const;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.model.YmChildrenOrderVaccination;
import com.cn.springcloudwechat.model.YmStationVaccine;
import com.cn.springcloudwechat.model.vaccine.YmVaccine;
import com.cn.springcloudwechat.model.vaccine.YmVaccineType;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import com.cn.springcloudwechat.service.vaccine.vaccineService.VaccineService;
import com.cn.springcloudwechat.service.vaccinstation.YmStationVaccineService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 疫苗服务站关联controller
 *
 * @author: Administrator
 * @create: 2018/8/31 17:47
 */

@RestController
@RequestMapping("/web/stationVaccine")
public class YmStationVaccineWebController {
    @Autowired
    YmStationVaccineService ymStationVaccineService;
    @Autowired
    YmUserPointService ymUserPointService;
    @Autowired
    VaccineService vaccineService;


    /**
     * 添加关联关系 (单个)
     *
     * @param ajaxRes
     * @param record
     * @return
     */
    @PostMapping("/insertSelective")
    public AjaxRes insertSelective(AjaxRes ajaxRes, YmStationVaccine record) {
        ymStationVaccineService.insertSelective(record);
        ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 查询疫苗服务站关联关系
     *
     * @param ajaxRes
     * @param stationVaccineId
     * @return
     */
    @GetMapping("/selectByPrimaryKey")
    public AjaxRes selectByPrimaryKey(AjaxRes ajaxRes, @RequestParam Long stationVaccineId) {
        YmStationVaccine ymStationVaccine = ymStationVaccineService.selectByPrimaryKey(stationVaccineId);
        ajaxRes.setSucceed(ymStationVaccine);
        return ajaxRes;
    }

    /**
     * 更新疫苗服务站关联关系表
     * @param ajaxRes
     * @param list
     * @param stationId
     * @return
     */
    @PostMapping("/updateByPrimaryKeySelective")
    public AjaxRes updateByPrimaryKeySelective(AjaxRes ajaxRes,@RequestParam("list") String list,@RequestParam Long stationId ) {
        String [] strarr=list.split(",");
       List<Long>result =new ArrayList<Long>();
        for (String str: strarr) {
            result.add(Long.parseLong(str));
        }
        ymStationVaccineService.updateByPrimaryKeySelective(result,stationId);
        ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 批量添加疫苗服务站关联关系
     *
     * @param ajaxRes
     * @param stationId
     * @param vaccineTypeId
     * @return
     */
    @PostMapping("/insertYmStationVaccineBatch")
    public AjaxRes insertYmStationVaccineBatch(AjaxRes ajaxRes, @RequestParam Long stationId,@RequestParam("vaccineTypeId") String  vaccineTypeId) {
       String [] strarr=vaccineTypeId.split(",");
        List<Long>result =new ArrayList<Long>();
        for (String str: strarr) {
            result.add(Long.parseLong(str));
        }
        ymStationVaccineService.insertYmStationVaccineBatch(stationId, result);
        ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        return ajaxRes;
    }

    /**
     * 查询服务站下所有疫苗
     *
     * @param ajaxRes
     * @param stationId
     * @param vaccineName
     * @return
     */
    @GetMapping("/getVaccineTypeByStationId")
    public AjaxRes getVaccineTypeByStationId(AjaxRes ajaxRes, @RequestParam Long stationId, String vaccineName) {
        List<YmVaccineType> result = ymStationVaccineService.getVaccineTypeByStationId(stationId, vaccineName);
        ajaxRes.setSucceed(result);
        return ajaxRes;
    }

    /**
     * 根据疫苗类型查询疫苗剂次
     *
     * @param ajaxRes
     * @param childeId
     * @param vaccineTypeId
     * @return
     */
    @GetMapping("/getYmVaccineByVaccineTypeId")
    public AjaxRes getYmVaccineByVaccineTypeId(AjaxRes ajaxRes, @RequestParam Long childeId, @RequestParam Long vaccineTypeId) {
        List<YmVaccine> result = ymStationVaccineService.getYmVaccineByVaccineTypeId(childeId, vaccineTypeId);
        ajaxRes.setSucceed(result);
        return ajaxRes;
    }


    /**
     * 添加预约记录
     * @param ajaxRes
     * @param ymChildrenOrderVaccination
     * @param point
     * @return
     */
    @PostMapping("/insertYmOrderVaccination")
    public AjaxRes insertYmOrderVaccination(AjaxRes ajaxRes,YmChildrenOrderVaccination ymChildrenOrderVaccination,@RequestParam  Integer point ){
        ymChildrenOrderVaccination.setState((short)0);
        ymChildrenOrderVaccination.setOrderState((short)1);
     Integer number=   ymStationVaccineService.getRowNumber(new DateTime(
                ymChildrenOrderVaccination.getOrderTime()).toString("yyyy-MM-dd"),
                ymChildrenOrderVaccination.getStationId());
        ymChildrenOrderVaccination.setRowNumber(Long.parseLong((number+1)+""));
        vaccineService.insertYmChildrenOrderVaccinationSer(ymChildrenOrderVaccination);
        Long userId=  ThreadLocalUtil.getUser().getUserId();
        ymUserPointService.insertUserPoint(userId,"疫苗预约",null,"-"+point);
        ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        return ajaxRes;
    }

    /**
     *  更新预约状态
     * @param ajaxRes
     * @param orderVaccinationId
     * @return
     */
    @PostMapping("/updateUserOrderState")
    public AjaxRes updateUserOrderState(AjaxRes ajaxRes,@RequestParam Long orderVaccinationId){
        ymStationVaccineService.updateUserOrderState(orderVaccinationId);
        ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
        return ajaxRes;
    }

    /**
     *  查询所有预约
     * @param ajaxRes
     * @param childId
     * @return
     */
    @GetMapping("/queryVaccineOrderPlanAll")
    public AjaxRes queryVaccineOrderPlanAll(AjaxRes ajaxRes,@RequestParam Long childId){
         List<Map<String,Object>> result=   ymStationVaccineService.queryVaccineOrderPlanAllMapper(childId);
        ajaxRes.setSucceed(result);
        return ajaxRes;
    }

}
